cmake_minimum_required(VERSION 3.0)
project(examples
	VERSION 1.0.0
	# DESCRIPTION "libuv examples"
	# HOMEPAGE_URL "www.zhyingkun.com"
	LANGUAGES C CXX)

if("${CMAKE_BUILD_TYPE}" STREQUAL "")
	set(CMAKE_BUILD_TYPE "Debug")
endif()
message(STATUS "CMakeLists.txt for ${PROJECT_NAME}")
message(STATUS "CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")

if(APPLE)
	set(CMAKE_C_FLAGS         "-std=gnu89 -Wall -Wextra")
	set(CMAKE_C_FLAGS_DEBUG   "-g")
	set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
	set(CMAKE_C_FLAGS         "-std=gnu89 -Wall -Wextra")
	set(CMAKE_C_FLAGS_DEBUG   "-g")
	set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
elseif(WIN32)
	set(CMAKE_C_FLAGS         "-Wall")
	set(CMAKE_C_FLAGS_DEBUG   "")
	set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
endif()

include_directories(../libuv/include)

if(NOT WIN32)

	set(CGI_SRC ./cgi/main.c)
	source_group(src FILES ${CGI_SRC})
	add_executable(cgi ${CGI_SRC})
	target_link_libraries(cgi libuv)
	set_target_properties(cgi PROPERTIES FOLDER "examples/cgi")
	set(TICK_SRC ./cgi/tick.c)
	source_group(src FILES ${TICK_SRC})
	add_executable(tick ${TICK_SRC})
	set_target_properties(tick PROPERTIES FOLDER "examples/cgi")

	set(MULTI_ECHO_SERVER_SRC ./multi-echo-server/main.c)
	source_group(src FILES ${MULTI_ECHO_SERVER_SRC})
	add_executable(multi-echo-server ${MULTI_ECHO_SERVER_SRC})
	target_link_libraries(multi-echo-server libuv)
	set_target_properties(multi-echo-server PROPERTIES FOLDER "examples/multi-echo-server")
	set(WORKER_SRC ./multi-echo-server/worker.c)
	source_group(src FILES ${WORKER_SRC})
	add_executable(worker ${WORKER_SRC})
	target_link_libraries(worker libuv)
	set_target_properties(worker PROPERTIES FOLDER "examples/multi-echo-server")

	set(PROC_STREAMS_SRC ./proc-streams/main.c)
	source_group(src FILES ${PROC_STREAMS_SRC})
	add_executable(proc-streams ${PROC_STREAMS_SRC})
	target_link_libraries(proc-streams libuv)
	set_target_properties(proc-streams PROPERTIES FOLDER "examples/proc-streams")
	set(TEST_SRC ./proc-streams/test.c)
	source_group(src FILES ${TEST_SRC})
	add_executable(stream-test ${TEST_SRC})
	set_target_properties(stream-test PROPERTIES FOLDER "examples/proc-streams")

	source_group(src FILES ./uvwget/main.c)
	add_executable(uvwget ./uvwget/main.c)
	target_link_libraries(uvwget libuv curl)
	set_target_properties(uvwget PROPERTIES FOLDER "examples")

	set(ONLY_MAIN_C_NOT_WIN interfaces progress queue-work queue-cancel signal thread-create tty 
	tty-gravity udp-dhcp uvcat uvtee)
	foreach(DEMO_DIR ${ONLY_MAIN_C_NOT_WIN})
		source_group(src FILES ./${DEMO_DIR}/main.c)
		add_executable(${DEMO_DIR} ./${DEMO_DIR}/main.c)
		target_link_libraries(${DEMO_DIR} libuv)
		set_target_properties(${DEMO_DIR} PROPERTIES FOLDER "examples")
	endforeach(DEMO_DIR)

endif(NOT WIN32)

set(PLUGIN_SRC ./plugin/main.c)
source_group(src FILES ${PLUGIN_SRC})
add_executable(plugin ${PLUGIN_SRC})
target_link_libraries(plugin libuv)
set_target_properties(plugin PROPERTIES FOLDER "examples/plugin")
# set(HELLO_SRC ./plugin/hello.c ./plugin/plugin.h)
# source_group(src FILES ${HELLO_SRC})
# add_library(hello MODULE ${HELLO_SRC})
# set_target_properties(hello PROPERTIES FOLDER "examples/plugin")

set(ONLY_MAIN_C_DEMO detach dns helloworld idle-basic idle-compute locks onchange 
	pipe-echo-server ref-timer spawn tcp-echo-server uvstop)
foreach(DEMO_DIR ${ONLY_MAIN_C_DEMO})
	source_group(src FILES ./${DEMO_DIR}/main.c)
	add_executable(${DEMO_DIR} ./${DEMO_DIR}/main.c)
	target_link_libraries(${DEMO_DIR} libuv)
	set_target_properties(${DEMO_DIR} PROPERTIES FOLDER "examples")
endforeach(DEMO_DIR)
